package com.css.fxfzfxqh.modules.seismicHazardData.controller;

import cn.hutool.core.io.FileUtil;
import com.css.fxfzfxqh.base.response.RestResponse;
import com.css.fxfzfxqh.modules.seismicHazardData.entity.PsHazardDataEntity;
import com.css.fxfzfxqh.modules.seismicHazardData.param.psHazard.ExportExcelParam;
import com.css.fxfzfxqh.modules.seismicHazardData.param.psHazard.PageParam;
import com.css.fxfzfxqh.modules.seismicHazardData.param.psHazard.RecordPageParam;
import com.css.fxfzfxqh.modules.seismicHazardData.param.psHazard.RegionParam;
import com.css.fxfzfxqh.modules.seismicHazardData.service.PsHazardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 发生概率地震危险性
 */
@RestController
@RequestMapping("psHazard")
@Slf4j
public class PsHazardController {

    @Autowired
    private PsHazardService psHazardService;

    /**
     * 导入excel
     *
     * @param file
     * @return
     */
    @PostMapping("importExcel")
    public RestResponse importExcel(@RequestParam("file") MultipartFile file,
                                    @RequestParam("type") Integer type,
                                    @RequestParam("ids") String fileIds,
                                    @RequestParam("year") String year,
                                    @RequestParam("excelFileId") String excelFileId) {
        try {
            if (!FileUtil.getSuffix(file.getOriginalFilename()).equals("xls") && !FileUtil.getSuffix(file.getOriginalFilename()).equals("xlsx")) {
                return RestResponse.fail("Excel文件格式不支持!");
            }
            return psHazardService.importExcel(file, type,fileIds,year,excelFileId);
        } catch (Exception e) {
            log.error("发生概率地震危险性excel: {} 导入失败:{} ", file.getName(), e.getMessage(), e);
            return RestResponse.fail("导入失败!");
        }
    }

    /**
     * 导出模板
     */
    @PostMapping("exportExcel")
    public void exportExcel(@RequestBody ExportExcelParam param, HttpServletResponse response) {
        try {
            Integer region = param.getRegion();
            List<RegionParam> regionValue = param.getRegionValue();
            String rangeValue = param.getRangeValue();
            Integer sheetType = param.getSheetType();
            psHazardService.exportExcel(sheetType, region, regionValue, rangeValue, response);
        } catch (Exception e) {
            log.error("导出模板,导出失败:{}", e.getMessage(), e);
        }
    }

    /**
     * 查询格网信息
     *
     * @param kilometerGridId
     * @return
     */
    @GetMapping("getByKilometerGridId")
    public RestResponse getByKilometerGridId(String kilometerGridId,Integer type) {
        try {
            return psHazardService.getByKilometerGridId(kilometerGridId,type);
        } catch (Exception e) {
            log.error("查询格网信息,查询失败:{},id:{}", e.getMessage(), kilometerGridId, e);
            return RestResponse.fail("查看失败!");
        }
    }

    /**
     * 根据省市县id查询空间数据
     */
    @GetMapping("getByRegionId")
    public RestResponse getByRegionId(){
        try {
            return psHazardService.getByRegionId();
        } catch (Exception e) {
            log.error("get getByRegionName error:{}",e.getMessage(),e);
            return RestResponse.fail("查看失败!");
        }
    }

    /**
     * 分页
     * @return
     */
    @GetMapping("page")
    public RestResponse page(PageParam param){
        try {
            Map<String, Object> page = psHazardService.page(param);
            return RestResponse.succeed(page);
        } catch (Exception e) {
            log.error("get page error:{}",e.getMessage(),e);
            return RestResponse.fail("查看失败!");
        }
    }

    /**
     * 上传记录分页
     * @param param
     * @return
     */
    @GetMapping("uploadRecords")
    public RestResponse uploadRecords(RecordPageParam param){
        try {
            Map<String, Object> page = psHazardService.uploadRecords(param);
            return RestResponse.succeed(page);
        } catch (Exception e) {
            log.error("get uploadRecords error:{}",e.getMessage(),e);
            return RestResponse.fail("查看失败!");
        }
    }

    /**
     * 下载文件
     */
    @PostMapping("export")
    public void export(@RequestParam("ids") String ids,HttpServletResponse response){
        try {
            psHazardService.export(ids,response);
        } catch (Exception e) {
            log.error("导出失败,id:{}",ids,e.getMessage(),e);
        }
    }
}
